package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.pojo.TbBrand;
import com.leyou.item.pojo.TbCategory;
import com.leyou.item.mapper.TbCategoryMapper;
import com.leyou.item.pojo.TbCategoryBrand;
import com.leyou.item.service.TbCategoryBrandService;
import com.leyou.item.service.TbCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * <p>
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系 服务实现类
 * </p>
 *
 * @author leyou
 * @since 2020-02-29
 */
@Service
public class TbCategoryServiceImpl extends ServiceImpl<TbCategoryMapper, TbCategory> implements TbCategoryService {
    @Autowired
    private TbCategoryBrandService tbCategoryBrandService;

    @Override
    public List<CategoryDTO> findByParentId(Long pid) {
        // 构建查询条件
        QueryWrapper<TbCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", pid);
        // 接收返回结果
        List<TbCategory> tbCategories = this.getBaseMapper().selectList(wrapper);
        // 处理空指针异常
        if (CollectionUtils.isEmpty(tbCategories)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        // 转换数据 向前端展示
        return BeanHelper.copyWithCollection(tbCategories, CategoryDTO.class);

    }


    @Override
    public List<TbCategory> selectCateByBrandid(Long brandId) {
        // 查询数据
        // 方式一：多表查询
        List<TbCategory> list = baseMapper.selectCateByBrandid(brandId);
        /*
         * // 方式二 两次查询
         * // 1、查询中间表数据
         * QueryWrapper<TbCategoryBrand> queryWrapper = new QueryWrapper<>();
         * queryWrapper.lambda().eq(TbCategoryBrand::getBrandId, brandId);
         * List<TbCategoryBrand> categoryBrandList = tbCategoryBrandService.list(queryWrapper);
         * // 转换id数据,lambda  表达式
         * Stream<Long> objectStream = categoryBrandList.stream().map(tbCategoryBrand -> tbCategoryBrand.getCategoryId());
         * System.out.println(Arrays.toString(objectStream.toArray()));
         * // 2、根据分类id查询分类数据,id已经查到，不再继续写了
         */

        return list;
    }

    /**
     * TODO 根据商品分类ids，查询商品分类，。需要一个根据多级分类id查询分类的接口
     * 用于搜索时 拼接 goods的 all字段
     *
     * @param ids
     * @return
     */
    @Override
    public List<CategoryDTO> findByIds(List<Long> ids) {
        List<TbCategory> tbCategories = (List<TbCategory>) this.listByIds(ids);
        if (CollectionUtils.isEmpty(tbCategories)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        List<CategoryDTO> list = BeanHelper.copyWithCollection(tbCategories, CategoryDTO.class);
        return list;
    }
}
